<template>
  <section class="cms-body" v-loading="loading">
    <!-- 返回组件 -->
    <cms-back></cms-back>
    <el-form ref="form" :model="dataInfo" :rules="rules" class="cms-form" label-width="162px">

      <el-form-item label="资源名称" class="flex-50" prop="resourceName">
        <el-input v-model="dataInfo.resourceName" class="cms-width" :maxlength="20"></el-input>
      </el-form-item>

	    <el-form-item label="资源类型" class="flex-50" prop="resourceType">
		    <el-input :disabled="true" v-model="dataInfo.resourceType.typeName"
		              class="cms-width" :maxlength="20"></el-input>
	    </el-form-item>

	    <el-form-item label="资源学科" class="flex-50" prop="resourceType">
		    <el-input :disabled="true" v-model="dataInfo.resourceSubject.subjectName"
		              class="cms-width" :maxlength="20"></el-input>
	    </el-form-item>

	    <el-form-item label="资源专业" class="flex-50" prop="resourceType">
		    <el-input :disabled="true" v-model="dataInfo.resourceMajor.majorName"
		              class="cms-width" :maxlength="20"></el-input>
	    </el-form-item>

	    <el-form-item label="资源格式" class="flex-50" prop="resourceType">
		    <el-input :disabled="true" v-model="dataInfo.resourceFormat.formatName"
		              class="cms-width" :maxlength="20"></el-input>
	    </el-form-item>

      <el-form-item label="状态" class="flex-50" prop="status">
        <el-switch
          v-model="dataInfo.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-value="1"
          inactive-value="0" @change="changeUpdate()"
        >
        </el-switch>
      </el-form-item>

      <el-form-item label="附件" class="flex-50" prop="enclosure">
        <scx-multiple-upload @change="getAttachments" :propList="enclosure"></scx-multiple-upload>
      </el-form-item>

<!--TODO -->
<!--      <el-form-item label="上传人" class="flex-50" >-->
<!--        <el-input v-model="dataInfo.uploadUser" class="cms-width" :maxlength="20"></el-input>-->
<!--      </el-form-item>-->

      <div class="form-footer">
        <el-button type="warning" v-if="this.id==0" @click="add(true)">保存并继续添加</el-button>
        <el-button type="primary" v-if="this.id==0" @click="add(false)">提交</el-button>
        <el-button type="primary" v-if="this.id!=0" @click="update()">修改</el-button>
        <el-button type="info" @click="$reset">重置</el-button>
        <span class="gray" v-if="this.id==0">
              选择“提交并继续添加”按钮会停留在添加页面；选择“提交”按钮后将跳转到对应的列表页</span>
      </div>
    </el-form>

  </section>
</template>

<script>
  import listMixins from '@/mixins/form';
  import axios from "axios";
  import va from "@/rules";
  import {Encrypt} from "@/untils/aes";

  export default {
    mixins: [listMixins],
    data() {
      let required = va.required();

      return {
        enclosure: [],
        resourceTypeGroup: [],
        rules: {
          //校验规则
          resourceName: [required],
          enclosure: [required],
          resourceStatus: [required],
          status: [required],
          resourceType: [required]
        }
      };
    },
    methods: {

      getDataInfo(id) {
        this.enclosure = [];
        if (id != 0) {
          this.loading = true;
          axios.post(this.$api.publicResourceManageGet, {id: id}).then(res => {
            this.loading = false;
            if (res.code == '200') {
              this.dataInfo = res.body;
              if (this.dataInfo.resourceUrl.trim() != '') {
                let tmp = {name: this.dataInfo.fileName, path: this.dataInfo.resourceUrl};
                this.enclosure.push(tmp);
              }
              if (id == 0) {
                this.rules.teacherName = [va.required()];
                this.rules.appKey = [va.required()];
              }
            }

          }).catch(error => {
            this.loading = false;
          })
        } else {
          this.dataInfo.resourceName = "";
          this.dataInfo.resourceType = "";
          this.dataInfo.resourceStatus = "1";
          this.dataInfo.status = "1";
          this.enclosure = [];
          this.loading = false;
        }

      },
      update() {
        if (this.enclosure.length !== 0) {
          this.dataInfo.fileName = this.enclosure[0].name;
          this.dataInfo.resourceUrl = this.enclosure[0].path;
        } else {
          this.$message.error("附件不能为空")
          this.dataInfo.fileName = "";
          this.dataInfo.resourceUrl = "";
          return
        }
        //FIXME: 此处方法为 hack 有误
        delete this.dataInfo.resourceTypeManage
        delete this.dataInfo.user

        this.updateDataInfo(this.$api.publicResourceManageUpdate, this.dataInfo, "list");
      },
      add(state) {
        if (this.enclosure.length !== 0) {
          this.dataInfo.fileName = this.enclosure[0].name;
          this.dataInfo.resourceUrl = this.enclosure[0].path;
        } else {
          this.$message.error("附件不能为空")
          this.dataInfo.fileName = "";
          this.dataInfo.resourceUrl = "";
          return
        }
        this.saveDataInfo(state, this.$api.publicResourceManageSave, this.dataInfo, "list");
      },
      getAttachments(res) {
        this.enclosure = res;//附件对象数组
      },
      changeUpdate() {
        this.$forceUpdate();
      },
      getResourceTypeGroup() {

        this.loading = true;
        axios.post(this.$api.resourceTypeManageGroup).then(res => {
          this.loading = false;
          if (res.code == '200') {
            this.resourceTypeGroup = res.body;
          }
        }).catch(error => {
          this.loading = false;
        })

      },
    },
    created() {
      //初始获取数据
      this.getDataInfo(this.id);
    }
  };
</script>

<style>

</style>
